package com.zbkj.crmeb.user.controller;

import com.alibaba.excel.EasyExcel;
import com.common.BeanMapper;
import com.common.CommonPage;
import com.common.CommonResult;
import com.common.PageParamRequest;
import com.constants.Constants;
import com.exception.CrmebException;
import com.google.common.collect.Lists;
import com.utils.DateUtil;
import com.zbkj.crmeb.store.model.excel.UserInviteRewardExcelModel;
import com.zbkj.crmeb.user.model.UserInviteReward;
import com.zbkj.crmeb.user.model.UserLevelGood;
import com.zbkj.crmeb.user.model.UserMonthBuy;
import com.zbkj.crmeb.user.request.UserLevelGoodRequest;
import com.zbkj.crmeb.user.request.UserLevelGoodSearchRequest;
import com.zbkj.crmeb.user.request.UserMonthBuyRequest;
import com.zbkj.crmeb.user.service.UserInviteRewardService;
import com.zbkj.crmeb.user.service.UserLevelGoodService;
import com.zbkj.crmeb.user.service.UserMonthBuyService;
import com.zbkj.crmeb.user.vo.UserMonthBuyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.net.URLEncoder;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户复购奖级表 前端控制器 +---------------------------------------------------------------------- | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +---------------------------------------------------------------------- | Copyright (c) 2016~2020 https://www.crmeb.com All rights
 * reserved. +---------------------------------------------------------------------- | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +---------------------------------------------------------------------- | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/user/invite/reward")
@Api(tags = "用户复购奖级表")
public class UserInviteRewardController {

    @Autowired
    private UserInviteRewardService userInviteRewardService;
    @Autowired
    private UserMonthBuyService userMonthBuyService;

    /**
     * 月份奖励列表
     */
    @ApiOperation(value = "月份奖励列表")
    @RequestMapping(value = "/monthList", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserMonthBuyVO>> getList(@Validated UserMonthBuyRequest request,
                                                            @Validated PageParamRequest pageParamRequest) {
        CommonPage<UserMonthBuyVO> userLevelCommonPage = CommonPage.restPage(userMonthBuyService.getList(request, pageParamRequest));
        return CommonResult.success(userLevelCommonPage);
    }

    /**
     * 月份奖励详情列表
     */
    @ApiOperation(value = "月份奖励详情列表")
    @RequestMapping(value = "/inviteList", method = RequestMethod.GET)
    public CommonResult<CommonPage<UserInviteReward>> getList(@Validated UserInviteReward request,
                                                              @Validated PageParamRequest pageParamRequest) {
        CommonPage<UserInviteReward> userLevelCommonPage = CommonPage.restPage(userInviteRewardService.getList(request, pageParamRequest));
        return CommonResult.success(userLevelCommonPage);
    }

    @ApiOperation("奖励导出")
    @GetMapping(path = "/exportReward")
    public void export(@Validated UserMonthBuyRequest request, HttpServletResponse response) {
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/vnd.ms-excel");
            String localTime = DateUtil.nowDate(Constants.DATE_TIME_FORMAT_NUM);
            String fileName = URLEncoder.encode("奖励导出：" + localTime, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            List<UserMonthBuyVO> datas = userMonthBuyService.findAll(request);
            EasyExcel.write(response.getOutputStream(), UserInviteRewardExcelModel.class).sheet("sheet1").doWrite(transfData(datas));
        } catch (Exception e) {
            throw new CrmebException(e.getMessage());
        }
    }

    private List<UserInviteRewardExcelModel> transfData(List<UserMonthBuyVO> rewards) {
        List<UserInviteRewardExcelModel> datas = BeanMapper.mapList(rewards, UserInviteRewardExcelModel.class);
        datas.forEach(data -> {
            data.setBuyFlag(data.getBuyFlag().equals("0") ? "否" : "是");
        });
        return datas;
    }
}



